import 'package:flutter_app/network/Loading.dart';
import 'package:flutter_app/values/MyColors.dart';
import 'package:image_pickers/image_pickers.dart';
import 'package:video_compress/video_compress.dart';

export 'package:image_pickers/image_pickers.dart';

///
/// 使用 image_pickers 图片选择器
///
/// 2021年12月8日 15:57:18
/// 小米11 拍摄60s左右的视频会造成oom 后续需要废除，当时还是使用照片和视频预览功能
///
/// 2022年6月6日 16:45:37
///  解决OOM问题 在    <application 中添加
///                     android:hardwareAccelerated="false"
///                     android:largeHeap = "true"
///
class ImagePickersUtils {
  //私有构造函数
  ImagePickersUtils._internal();

  //保存单例
  static ImagePickersUtils _imagePickersUtils = ImagePickersUtils._internal();

  //工厂构造函数
  factory ImagePickersUtils() => _imagePickersUtils;

  ///选择多张图片 Select multiple images
  Future<List<Media>> selectImages({int selectCount = 9, bool showCamera = false}) async {
    List<Media> _listImagePaths = await ImagePickers.pickerPaths(
        galleryMode: GalleryMode.image,
        showCamera: showCamera,
        selectCount: selectCount,
        showGif: false,
        compressSize: 500,
        uiConfig: UIConfig(uiThemeColor: MyColors.FFC20E0B),
        //uiConfig: UIConfig(uiThemeColor: Color(0xffff0f50)),
        cropConfig: CropConfig(enableCrop: false, width: 2, height: 1));

    return _listImagePaths;
    /// 或者 or
    // ImagePickers.pickerPaths().then((List medias){
    //   /// medias 照片路径信息 Photo path information
    // });
  }

  ///选择多个视频 Select multiple videos
  Future<List<Media>> selectVideos({int selectCount = 9, bool showCamera = false}) async {
    List<Media> _listVideoPaths = await ImagePickers.pickerPaths(
      galleryMode: GalleryMode.video,
      showCamera: showCamera,
      selectCount: selectCount,
      showGif: false,
      compressSize: 500,
      uiConfig: UIConfig(uiThemeColor: MyColors.FFC20E0B),
    );
    return _listVideoPaths;
  }

  ///选择多个图片和视频 Select multiple all
  Future<List<Media>> selectAll({int selectCount = 9, bool showCamera = false}) async {
    List<Media> _listVideoPaths = await ImagePickers.pickerPaths(
      galleryMode: GalleryMode.all,
      showCamera: showCamera,
      selectCount: selectCount,
      showGif: false,
      compressSize: 500,
      uiConfig: UIConfig(uiThemeColor: MyColors.FFC20E0B),
    );
    return _listVideoPaths;
  }

  ///直接打开相机拍摄图片 Open the camera directly to take a picture
  /// media 包含照片路径信息 Include photo path information
  Future<Media?> openCamera() async {
    Media? media = await ImagePickers.openCamera();
    /// 未拍照返回 media = null
    return media;

  }

  ///直接打开相机拍摄视频 Open the camera directly to take a picture
  Future<Media?> openCameraVideo() async {
    ///直接打开相机拍摄视频 Open the camera directly to take a picture
    Media? media = await ImagePickers.openCamera(cameraMimeType: CameraMimeType.video);
    Loading.show(status: '视频压缩中');
    /// 使用 video_compress 对视频 进行压缩
    MediaInfo? mediaInfo = await VideoCompress.compressVideo(
      media!.path!,
      quality: VideoQuality.DefaultQuality,
      deleteOrigin: false // 删除源文件
    );
    Loading.dismiss();
    media.path = mediaInfo!.path;

    /// 未拍照返回 media = null
    return media;
  }


  ///预览图片 Preview picture
  Future<void> previewImage(List _listImagePaths, int index) async {
    ImagePickers.previewImage(_listImagePaths[index].path);
  }

  ///预览网络图片 Preview picture
  Future<void> previewUrlImage(String url) async {
    ImagePickers.previewImage(url);
  }

  ///预览多张图片 Preview multiple pictures
  Future<void> previewImagesByMedia (List<Media> _listImagePaths, int index) async{
    ImagePickers.previewImagesByMedia(_listImagePaths, index);
  }

  ///预览多张图片 Preview multiple pictures
  Future<void> previewImages(List<String?> paths, int index) async {
    ImagePickers.previewImages(paths, index);
  }

  ///预览视频 Preview video
  Future<void> previewVideo(List<Media> _listVideoPaths, int index) async{
    ImagePickers.previewVideo(_listVideoPaths[index].path!);
  }

  ///预览网络视频  Preview video
  Future<void> previewVideoUrl(String url) async {
    ImagePickers.previewVideo(url);
  }

  ///保存图片到图库 Save image to gallery
  ///
  /// 返回保存的图片路径 Return the saved image path
  ///
  /// imageUrl 网络图片url Web image url
  Future<void> saveImageToGallery(String imageUrl) async {
    ImagePickers.saveImageToGallery(imageUrl);
  }

  ///保存视频到相册中 Save video to album
  ///
  /// 返回保存的视频路径 Android 返回保存的视频文件路径 ios 暂无法返回本地视频路径
  /// Return the saved video path Android returns the saved video file path ios can not return to the local video path
  ///
  /// videoUrl 网络视频url Web video url
  ///
  Future<void> saveVideoToGallery(String videoUrl) async {
    ImagePickers.saveVideoToGallery(videoUrl);
  }
}
